<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$errors = array();

if ($route->POST('action') == 'add')
{
	$name = UV::readMQ($route->POST("name"));
	$link = translitIt($name);
	$i = 1;
	while ($db->getCell("select `link` from `tip_sections` where `link` = '$link' limit 1")){
		$link = translitIt($name)."-".($i++);
	}
	$text = UV::readMQ($route->POST("text"));
	$order = UV::readInt($route->POST("order"));

	$query = "insert into `tip_sections` values(NULL,'$name','$link','text','$order')";
	
	$update = $db->sendQuery($query);
	if ($update){
		goToPage("/admin/sections/edit/".mysql_insert_id());
	} else {
		$errors[] = "Ошибка добавления записи";
	}
}

loadPage("head.php");
?>
<h1>Добавление раздела</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
?>
<form method='POST' id='edit'>
	<input type='hidden' name='action' value='add' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'><label for='name'>Название</label></td>
			<td width='600px'>
				<input type='text' name='name' id='name' value='<?php echo $route->POST('name'); ?>' />
			</td>
		</tr>
		<tr>
			<td><label for='text'>Текст</label></td>
			<td><textarea name='text' id='text'><?php echo $route->POST('text'); ?></textarea></td>
		</tr>
		<tr>
			<td><label for='order'>Порядок</label></td>
			<td><input type='text' name='order' class="short" id='order' value='<?php echo $route->POST('order'); ?>' /></td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Добавить' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>